package com.hugesoft.jxgs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hugesoft.jxgs.R;
import com.hugesoft.jxgs.activity.base.BaseActivity;
import com.hugesoft.jxgs.utils.CheckInputUtil;
import com.hugesoft.logic.managers.LogicManager;
import com.hugesoft.logic.managers.common.Constans;
import com.hugesoft.logic.module.DriverLicence;
import com.hugesoft.utils.Util;

public class AddLicenceActivity extends BaseActivity implements OnClickListener{
	EditText etDriverLicenceNo,etDriverName,etDriverType,etDriverAge;
	Button btnAdd;
	public static String TAGLICENCENO = "TAGLICENCENO";
	public static int RSTADDLICENCE = 1000;
	private Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			dismissDialog();
			switch (msg.what) 
			{
			case Constans.ADD_LICENCE_SUCCESS:
				Util.showToast(AddLicenceActivity.this,"添加驾照成功");
				DriverLicence licence = (DriverLicence)msg.obj;
				Intent intent = new Intent();
				intent.putExtra(AddLicenceActivity.TAGLICENCENO, licence.getLicenceno());
				setResult(AddLicenceActivity.RSTADDLICENCE,intent);
				finish();
				break;
			case Constans.ADD_LICENCE_FAILED:
				
				break;
			}
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initUI();
	}
	
	private void initUI()
	{
		setContentView(R.layout.activityaddlicence);
		((TextView) findViewById(R.id.titleTextView)).setText("新增驾照信息");
		findViewById(R.id.backImg).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		etDriverLicenceNo = (EditText)findViewById(R.id.etDriverLicenceNo);
		etDriverLicenceNo.setText(getIntent().getStringExtra(TAGLICENCENO));
		
		etDriverName = (EditText)findViewById(R.id.etDriverName);
		etDriverType = (EditText)findViewById(R.id.etDriverType);
		etDriverAge = (EditText)findViewById(R.id.etDriverAge);
		
		btnAdd = (Button)findViewById(R.id.btnAddLicence);
		btnAdd.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View arg0) {
		
		String licenceno =  etDriverLicenceNo.getText().toString();
		String drivername = etDriverName.getText().toString();
		String driverage = etDriverAge.getText().toString();
		String drivertype = etDriverType.getText().toString();
		
		if (!CheckInputUtil.islicencenoverified(licenceno)) 
		{
			Util.showToast(AddLicenceActivity.this, "驾照号码非法");
			return;
		}
		
		if (!CheckInputUtil.isnameverified(drivername) )
		{
			Util.showToast(AddLicenceActivity.this, "姓名非法");
			return;
		}
		
		if (!CheckInputUtil.isdrivertypeverified(drivertype)) 
		{
			Util.showToast(AddLicenceActivity.this, "驾驶类型非法");
			return;
		}
		
		if (!CheckInputUtil.isdriverageverified(driverage) )
		{
			Util.showToast(AddLicenceActivity.this, "驾龄非法");
			return;
		}
		
		LogicManager.driverLicenceManager().addlicenceinfo(licenceno, drivertype, drivername, Integer.parseInt(driverage), mHandler);
	}
}
